//角色清单 
const char1 = [
    [0, 'up精选名字', 'headpicsrc'],
    //[1,'佩可莉姆','https://patchwiki.biligame.com/images/pcr/6/67/rpznjh1epytf8mbp0t1dq91svg9dcti.png'],
    //[2,'凯露','https://patchwiki.biligame.com/images/pcr/3/39/lgcqculc5zh699k6f391kiwnwcn94me.png'],
    //[3,'可可萝','https://patchwiki.biligame.com/images/pcr/a/a4/mrwruc57npoamhw5y9ttd76s0l76xzd.png'],
    [4, '绫濑由加莉', 'https://patchwiki.biligame.com/images/pcr/c/cd/mpjpf8q3fczqdn5stzx5epe7a1whb1n.png'],
    [5, '莉玛', 'https://patchwiki.biligame.com/images/pcr/0/04/lo60ot9xodirhcikbgsp5ys5j1semxf.png'],
    [6, '玉泉美咲', 'https://patchwiki.biligame.com/images/pcr/6/69/1295ipvt0ha6zly43nr690rj7qsl4py.png'],
    [7, '双叶碧', 'https://patchwiki.biligame.com/images/pcr/2/28/dwctebo5xojfp84t1dl8rzwf5semv7n.png'],
    [8, '天野铃莓', 'https://patchwiki.biligame.com/images/pcr/c/cc/gwnmpp3h76m69ewd0oqnl22gtmt6hje.png'],
    [9, '风宫依里', 'https://patchwiki.biligame.com/images/pcr/6/62/h81lrn1gzun78rl6l0354xauni9yqs0.png'],
    [10, '栗林胡桃', 'https://patchwiki.biligame.com/images/pcr/0/0f/nwn0d9rx7uo8zxbhxcrrx1s3v4ugkfc.png'],
    [11, '未奏希', 'https://patchwiki.biligame.com/images/pcr/1/1d/biyc1gryppcb7c5tzdvqti6bmcyd55w.png'],
    [12, '士条怜', 'https://patchwiki.biligame.com/images/pcr/3/33/gcfzk5nghr9krp81u4x59jsbpph41qr.png'],
    //[13,'草野优衣','https://patchwiki.biligame.com/images/pcr/1/18/h8ni7jp1oej5mk2al7v1rjofkd4nqis.png'],
    [14, '春咲日和莉', 'https://patchwiki.biligame.com/images/pcr/3/37/264hu9xzlhbahpe8tzexwysq3g13x3n.png'],

];
const char2 = [
    [0, 'up精选名字', 'headpicsrc'],
    [1, '风宫茜里', 'https://patchwiki.biligame.com/images/pcr/1/14/8pn703o0duodmiyou1c565wjczj5b5o.png'],
    [2, '出云宫子', 'https://patchwiki.biligame.com/images/pcr/6/62/nu4e4h9eopdciqg2va9hf4c8fqjzq3q.png'],
    [3, '虹村雪', 'https://patchwiki.biligame.com/images/pcr/c/c3/f8eexvpqyqkccbjad1uucs9z9whwnuy.png'],
    [4, '美波铃奈', 'https://patchwiki.biligame.com/images/pcr/e/e5/6d00c5yutzbikc8vnta72rjzppu5wvj.png'],
    [5, '喜屋武香织', 'https://patchwiki.biligame.com/images/pcr/5/59/516euuhwnsfoqvolt8usv5z1jq0kfov.png'],
    [6, '茜美美', 'https://patchwiki.biligame.com/images/pcr/4/41/fwg0kohgsjgf76yxk3t7uleary1rho4.png'],
    [7, '仓石惠理子', 'https://patchwiki.biligame.com/images/pcr/4/49/md183zh8ewq7lupcmo9smuymjw749cm.png'],
    [8, '上喜忍', 'https://patchwiki.biligame.com/images/pcr/e/e1/bh7jkgsmitpvxulkc6mc217pzge6jzx.png'],
    [9, '野户真阳', 'https://patchwiki.biligame.com/images/pcr/2/2e/o6c9lsjthg36mzdt7cysolok3cv3pvm.png'],
    [10, '柏崎栞', 'https://patchwiki.biligame.com/images/pcr/0/05/exnyc4y1iuhrbjg3gxe7fncrj2mevmh.png'],
    [11, '三角千歌', 'https://patchwiki.biligame.com/images/pcr/d/db/egxfjidgjrjc8uatgi30dhvwzb3hkwn.png'],
    [12, '远见空花', 'https://patchwiki.biligame.com/images/pcr/b/b5/672mvpqoxo64iv5t5dkr6vs31ko148k.png'],
    [13, '宫坂珠希', 'https://patchwiki.biligame.com/images/pcr/3/39/dvomjvmcbbyliifd5hvb97mmqwtocjf.png'],
    [14, '大神美冬', 'https://patchwiki.biligame.com/images/pcr/c/ce/fbvjbat2po9fy580a3jyj0z2q4tmf2l.png'],
    [15, '宵浜深月', 'https://patchwiki.biligame.com/images/pcr/e/ed/mez3hg9iq9s5h6gor8ih6om9n9ln2za.png'],
    [16, '森近铃', 'https://patchwiki.biligame.com/images/pcr/0/02/al8yiod9vehdtaov9pgs41uovntnkr2.png'],

];
//限定角色请以1000叠加
const char3 = [
    [1, '柊杏奈', 'https://patchwiki.biligame.com/images/pcr/7/72/edy145bksvw972yhfpfgp8qbquhsi1j.png'],
    [2, '姬宫真步', 'https://patchwiki.biligame.com/images/pcr/7/72/jb3637kcjwh7z9559n3ca377tkldr0t.png'],
    [3, '衣之咲璃乃', 'https://patchwiki.biligame.com/images/pcr/a/a5/jsara36yjh1e8kj1srxtkztghdhmljd.png'],
    [4, '柏崎初音', 'https://patchwiki.biligame.com/images/pcr/b/ba/e1q3ot9mhbtqh7nsgpnc5bp39idld6s.png'],
    [5, '支仓伊绪', 'https://patchwiki.biligame.com/images/pcr/8/82/a1pquxl61ls6ahhinzptb63tvadus03.png'],
    [6, '佐佐木咲恋', 'https://patchwiki.biligame.com/images/pcr/8/8c/dy0rtln3fvhxkkqfxsnbnws61k8qqjh.png'],
    [7, '樱井望', 'https://patchwiki.biligame.com/images/pcr/e/e3/ccy9fdczpyc94sfm20q7uucw6g0vm5j.png'],
    [8, '妮侬・朱贝尔', 'https://patchwiki.biligame.com/images/pcr/9/91/4h2e4w8p876oxtcu1xv2e9anb16od6s.png'],
    [9, '藤堂秋乃', 'https://patchwiki.biligame.com/images/pcr/a/a9/hj9uxkawbpguzfrpkrr6ymjwff6vmym.png'],
    [10, '安芸真琴', 'https://patchwiki.biligame.com/images/pcr/2/27/ergoy0815kqjqqpqn3sa0yuz2be4pco.png'],
    [11, '白银纯', 'https://patchwiki.biligame.com/images/pcr/9/98/4qostpj00cvuozwjjl2po690k9iu994.png'],
    [12, '星野静流', 'https://patchwiki.biligame.com/images/pcr/0/00/0koylmjvym7s63y8xmz9skaa1xfuo0t.png'],
    [13, '莫妮卡·拜斯温特', 'https://patchwiki.biligame.com/images/pcr/0/03/smxy5tsyd5t92fqk5onwy56hlvbobb1.png'],
    [14, '姬塔', 'https://patchwiki.biligame.com/images/pcr/7/7f/j62dyqwinraqa1jhcey51t2xgpl3kkq.png'],
    [15, '亚里莎', 'https://patchwiki.biligame.com/images/pcr/7/7c/9mlr4cmt1yol6qam0k5eetgqpoy9wao.png'],
    [1001, '凯露（夏日）', 'https://patchwiki.biligame.com/images/pcr/8/8f/42rzcdui2rtnpf4je2h94wyz45k5kue.png'],
    [1002, 'debug', 'https://patchwiki.biligame.com/images/pcr/8/8f/42rzcdui2rtnpf4je2h94wyz45k5kue.png'],
    [1003, 'debuf', 'https://patchwiki.biligame.com/images/pcr/8/8f/42rzcdui2rtnpf4je2h94wyz45k5kue.png'],

];
var times = 0;
//datavefity bs ms 3xchl rmb 2x 3x 
let bs = ms = chl = rmb = x2 = x3 = 0;
jQuery(document).ready(function () {
    console.log("模块：公主连结扭蛋模拟器已经成功载入");
    $('#gachaset').append('<font color = "darkpink"><select id = "setUP" onchange="cgchance(this.value)" class="combobox"><option value ="1">2.5%</option><option value ="2">5%</option><option value ="3">2.5%(1.4%UP)</option></select></font>');
    fill3x();
    if (!isPc()) {
        alert("当前为非PC端访问，页面排版可能不尽人意，望谅解");
    }
});
//获取随机数（默认10000内）
function getRand(a = 10000) {
    let tmp = Math.ceil(Math.random() * a);
    return tmp;
}
//数值输出至页面
function setVar(Gbs, Gms, Gchl, Gcs, Grmb) {
    $("#gacha_bs").html(Gbs);
    $("#gacha_ms").html(Gms);
    $("#gacha_chl").html(Gchl);
    $("#gacha_cs").html(Gcs);
    $("#gacha_rmb").html(Grmb);
}
//判断是否为PC端
function isPc() {
    var UserAgent = navigator.userAgent.toLowerCase();
    var isiPad = UserAgent.match(/ipad/i) == "ipad";
    var isiPhone = UserAgent.match(/iphone os/i) == "iphone os";
    var isUc = UserAgent.match(/ucweb/i) == "ucweb";
    var isAndroid = UserAgent.match(/android/i) == "android";
    var iswechat = UserAgent.match(/MicroMessenger/i) == "micromessenger";
    if (!(isAndroid || isUc || isiPad || isiPhone || iswechat))
        return true;
    else
        return false;
}

$("#reset").click(function () {
    resetgacha();
    fill3x();
});

//是否触发10连保底
var gacha_bd = true;
//单抽一次
$("#gacha1").click(function () {
    cleartb();
    result = [[0, 0]];
    gacha();
    times++;
    consumecal();
    setVar(bs, ms, chl, times, rmb);
    filltb();
});

$("#gacha10").click(function () {
    cleartb();
    result = [[0, 0]];
    gacha_bd = true;
    times += 10;
    for (i = 1; i <= 9; i++) {
        gacha();
    }
    gacha(gacha_bd);
    consumecal();
    setVar(bs, ms, chl, times, rmb);
    filltb();
});


var char3bak = [];

char3bak = clone(char3);


//选中角色清单 (是否为限定角色 ，表池id)
var charSel = [];
//中奖几率 3x 2x 特殊模式
let chance = [250, 8200, 0];
function cgchance(a) {
    switch (parseInt(a)) {
        case 1: chance = [250, 8200, 0]; break;//2.5
        case 2: chance = [500, 8200, 0]; break;//5
        case 3: chance = [250, 8200, 1]; break;//2.5 with 1.4up
    }
}
//扭蛋

let result = [[0, 0]];
function gacha(a = false) {
    var rand = getRand();
    if (!a) {
        if (rand <= chance[0]) {//pick 3x
            ms += 50;
            x3++;
            result.push([3, pickchar(3, rand)]);
            gacha_bd = false;
        } else if (rand > chance[0] && rand <= chance[1]) {//pick 1x
            ms += 1;
            result.push([1, pickchar(1, rand)]);
        } else {//pick 2x
            ms += 10;
            x2++;
            result.push([2, pickchar(2, rand)]);
            gacha_bd = false;
        }
    } else {//触发保底
        if (rand <= chance[0]) {//pick 3x
            ms += 50;
            x3++;
            result.push([3, pickchar(3, rand)]);
        } else {//pick 2x
            ms += 10;
            x2++;
            result.push([2, pickchar(2, rand)]);
        }
    }
}
//从角色列表中获取一个角色
function pickchar(a, b) {
    var rand = 0;
    switch (a) {
        case 1: {
            rand = getRand(char1.length - 1);
            return rand;
        }
        case 2: {
            rand = getRand(char2.length - 1);
            return rand;
        }
        case 3: {
            //确认三星UP角色的概率 普通模式默认0.7 in 2.5  特殊模式1.4 in 2.5
            if (getRand() < ((chance[2] == 0) ? 2800 : 5600 )&& charSel.length > 0) {
                let tmp = getRand(charSel.length) - 1;
                return charSel[tmp];
            }
            else {
                let tmp = getRand(char3bak.length - 1);
                do {
                    tmp = getRand(char3bak.length - 1);
                } while (char3bak[tmp][0] > 1000)
                return tmp;
            }
        }
    }
}
//计算消耗
function consumecal() {
    bs = times * 150;
    chl = (x3 / times * 100).toFixed(3);
    rmb = (times * (15.54)).toFixed(2);
}
//将抽卡结果填充至panel区
function filltb() {
    num = 0;
    for (i = 0; i < result.length; i++) {
        switch (result[i][0]) {
            case 1: {
                num = result[i][1];
                let str = "<span style='display: inline-block ;margin: 5% 4%;'> <a href='https://wiki.biligame.com/pcr/" + char1[num][1] + "' target='_block'><img width='85px' height='85px'src='" + char1[num][2] + "' style='margin: auto;border: 4px solid; border-color: rgb(167, 167, 167); border-radius: 10px;'></a></span>"
                $("#gacharesult").append(str);
                break;
            }
            case 2: {
                num = result[i][1];
                let str = "<span style='display: inline-block ;margin: 5% 4%;'> <a href='https://wiki.biligame.com/pcr/" + char2[num][1] + "' target='_block'><img width='85px' height='85px'src='" + char2[num][2] + "' style='margin: auto;border: 4px solid; border-color: rgb(255, 227, 104); border-radius: 10px;'></a></span>"
                $("#gacharesult").append(str);
                break;
            }
            case 3: {
                num = getlocale(result[i][1]);
                //console.log("实际位置："+num +"之前位置 ："+result[i][1]);
                let str = "<span style='display: inline-block ;margin: 5% 4%;'> <a href='https://wiki.biligame.com/pcr/" + char3[num][1] + "' target='_block'><img class='got3x' width='85px' height='85px'src='" + char3[num][2] + "' style='margin: auto;border: 4px solid; border-color: rgb(255, 129, 188); border-radius: 10px;'></a></span>"
                $("#gacharesult").append(str);

                let nodes = $(".chara3xnum");
                nodes.each(function () {
                    if ($(this).attr("no") == char3[num][0]) {
                        $(this).html(1 + parseInt($(this).text()));
                    }
                })
                break;
            }
        }
    }
}
//清空panel区
function cleartb() {
    $("#gacharesult").html("");
}


//重置功能
function resetgacha() {
    times = 0;
    bs = ms = chl = rmb = x2 = x3 = 0;
    setVar(bs, ms, chl, times, rmb);
    $("#all3x").html("");
    cleartb();
}

//将所有三星绘制面板并增加功能
function fill3x() {
    let node = $('#all3x');
    charSel = [];
    char3bak = clone(char3);
    //点选UP复原
    node.html("");
    char3.forEach(char => {
        let jet = '<span class="chara3x"  no=' + char[0] + ' sel="0"><img src="' + char[2] + '"><span class="chara3xnum" no=' + char[0] + '>0</span></span>';
        node.append(jet);
    });
}

//点选UP角色
$("#all3x").on("click", ".chara3x", function () {
    let no = $(this).attr("no");
    if ($(this).attr("sel") == "0") {
        $(this).css("border-color", "red");
        $(this).attr("sel", "1");
    } else {
        $(this).css("border-color", "lightsalmon");
        $(this).attr("sel", "0");
    }
    //动态修改UP选单
    char3bak.forEach((char) => {
        if (char[0] == no) {
            charSel.push(no);
            char[0] = parseInt(5000 + no);
            return;
        } else if (char[0] == (5000 + no)) {
            char[0] = parseInt(no);
            var pos = $.inArray(no, charSel);
            charSel.splice(pos, 1);
            return;
        }
    })
});

//获取角色实际点位
function getlocale(loc) {
    for (let i = 0; i < char3.length; i++) {
        if (char3[i][0] == loc) {
            return i;
        }
    }
}

//深度克隆
function clone(source) {
    return JSON.parse(JSON.stringify(source));
}